class code2 {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> ret = new ArrayList<>();

        int left = 0;
        int right = 0;
        char[] ss = s.toCharArray();
        char[] pp = p.toCharArray();
        int len = p.length();

        int[] hash1 = new int[26];//s
        int[] hash2 = new int[26];//p

        //统计p的字符
        for(char ch : pp) {
            hash2[ch-'a']++;
        }
        int count = 0;//有效字符的个数
        while(right < s.length()) {
            //进窗口
            hash1[ss[right]-'a']++;
            if(hash1[ss[right]-'a'] <= hash2[ss[right]-'a']) {
                count++;
            }

            //判断
            if(right-left+1 > len) {
                //出窗口
                if(hash1[ss[left]-'a'] <= hash2[ss[left]-'a']) {
                    count--;
                }
                hash1[ss[left++]-'a']--;
            }
            if(len == count) {
                ret.add(left);
            }
            right++;
        }
        return ret;
    }
}